/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.group.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;


import net.comm.page.PageCtr;
import net.comm.group.service.impl.GMemberServiceImpl;
import net.comm.group.service.impl.GdianpingServiceImpl;
import net.comm.group.service.impl.GpartyServiceImpl;
import net.comm.group.service.impl.GsignupServiceImpl;
import net.comm.group.web.form.GMemberForm;
import net.comm.group.web.form.GdianpingForm;
import net.comm.group.web.form.GpartyForm;
import net.comm.group.web.form.GsignupForm;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 *活动Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GpartyAction extends AbstractBaseAction {
	
	private GpartyServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD = "list";
	final String INDEX_FORWARD = "index";
	final String INFO_FORWARD = "info";
	final String OPER_FORWARD = "oper";
	final String ADLIST_FORWARD = "adlist";
	final String ADOPER_FORWARD = "adoper";	
	final String LOGIN_FORWARD = "loginFwd";	
	
	/**
	 *构造方法
	 */
	public GpartyAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new GpartyServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询活动清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GpartyForm theForm = (GpartyForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		
		/**
		 *赋值查询条件
		 */
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("partylist" , list);
		request.setAttribute("theForm", theForm);

		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	/**
	 * 查询我加入的活动清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward listmyAdd(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GpartyForm theForm = (GpartyForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,20));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *赋值查询条件
		 */
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.listMyAdd(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		System.out.println("s="+ list.size());
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "listmyAdd");
		request.setAttribute("partylist" , list);
		request.setAttribute("theForm", theForm);

		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}	
	/**
	 * 活动首页
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward index(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GpartyForm theForm = (GpartyForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,10));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *取得推荐活动
		 */
		List recommlist = service.adlist(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("recommlist" , recommlist);
		
		
		/**
		 *取得最新活动
		 */

		List lastlist = service.adlist(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		setAction(request, "index");
		request.setAttribute("lastlist" , lastlist);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(INDEX_FORWARD);
	}	
	/**
	 * 查询活动清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GpartyForm theForm = (GpartyForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		
		/**
		 *赋值查询条件
		 */
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "adlist");
		request.setAttribute("partylist" , list);
		request.setAttribute("theForm", theForm);

		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		//判断当前用户是否已经加入了该群组
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);

		String addflg = "no";
		if(userID != null){
			GMemberServiceImpl memservice = new GMemberServiceImpl();
			GMemberForm memForm = new GMemberForm();
			
			memForm.setGroupID(theForm.getQryGroupID());
			memForm.setUserID(userID);
			
			memservice.get(memForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS ){
				if(msg.getMsg().equals("会员不存在"))
				{
					request.setAttribute("addflg", addflg);
				}
				msg.setErrCode(GlbFinal.ERRCODE_SUCCESS);	
			}else{
				//当前用户类型（群组创建者或者管理员）
				request.setAttribute("memType", memForm.getType());
				addflg = "yes";
				request.setAttribute("addflg", addflg);
			}

		}else{
			
			request.setAttribute("addflg", addflg);
		}		
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}

	/**
	 * 跳转到显示页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
	
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID == null || userID == 0){
			
			return mapping.findForward(LOGIN_FORWARD);
		}
		theForm.setUserID(userID);		
		theForm.setStatus(GlbFinal.STS_NORMAL);
		theForm.setSignNum(1);
		theForm.setWork_location(1962);
		theForm.setWork_sublocation(1987);
		
		request.setAttribute("theForm", theForm);

		return mapping.findForward(OPER_FORWARD);
	}
	
	/**
	 *增加活动
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.add(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);

	}
	/**
	 * 跳转到显示页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;

		return get(mapping, form, request, response);
	}
	/**
	 *取得活动内容
	 */
	public ActionForward info(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		
		request.setAttribute("theForm", theForm);
		
		//想参加这个活动的人
		GsignupServiceImpl signupservice = new GsignupServiceImpl();
		GsignupForm signupForm = new GsignupForm();
		
		signupForm.setQryPartyID(theForm.getPartyID());
		signupForm.setPage(new PageCtr(1, 72, "N", " 1=1 "));
		
		List signuplist = signupservice.adlist(signupForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("signuplist", signuplist);	
		
		//判断当前浏览用户是否已经报名
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		String addflg = "no";
		if(userID != null){
			signupForm.Clear();
			signupForm.setPartyID(theForm.getPartyID());
			signupForm.setUserID(userID);
			
			
			signupservice.get(signupForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS ){
				request.setAttribute("addflg", addflg);
			}else{
				//当前用户已经加入活动
				addflg = "yes";
				request.setAttribute("addflg", addflg);
			}
			msg.setErrCode(GlbFinal.ERRCODE_SUCCESS);	
		}else{
			
			request.setAttribute("addflg", addflg);
		}			
		
		//取得相片点评信息
		GdianpingServiceImpl dianpingservice = new GdianpingServiceImpl();
		GdianpingForm dianpingform = new GdianpingForm();
		
		dianpingform.setQryDsort(GlbFinal.DSORT_PARTY);
		dianpingform.setQryDsortID(theForm.getPartyID());
		
		List dpsumlist = dianpingservice.listGbyDsortID(dianpingform, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("dpsumlist" , dpsumlist);		
		
	
		
		
//		//相关活动
//		GpartyForm relGpartyForm = new GpartyForm();
//		
//		relGpartyForm.setPage(new PageCtr(1, 12, "N", " 1=1 "));
//		relGpartyForm.setQryFirsort(theForm.getQryFirsort());
//		
//		List relpartylist = service.adlist(relGpartyForm, msg);
//		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
//		{
//			HtmlMessageBox hmb = new HtmlMessageBox();
//			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
//			hmb.setPrintMessage(msg.getMsg());
//			hmb.setForwardUrl("javascript:history.go(-1)");
//			return showMsg(hmb , request , response , mapping);
//		}
//		request.setAttribute("relpartylist", relpartylist);		
		
		//活动评论
		
		
		//活动照片
		
		return mapping.findForward(INFO_FORWARD);
	}	
	/**
	 *取得活动内容
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("theForm", theForm);
		
		return mapping.findForward(OPER_FORWARD);
	}		
	/**
	 *修改活动
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
		
//		String path = request.getSession().getServletContext().getRealPath("/");

		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		theForm.setQryUserID(theForm.getUserID());
		theForm.setAction("adlist");
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);

	}

	/**
	 *删除活动
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GpartyForm theForm = (GpartyForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.delete(theForm,  msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(theForm.getGroupID());
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);

	}

}
